﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UIShare;
using EFong.Contract;
using EFong.View.Proxy;

namespace EFong.View.Customer
{
    public partial class Frm_Customer : BasicMdiChildForm
    {
        private Proxy.MasterServiceProxy _masterProxy {get{ return Proxy.MasterServiceProxy.GetInstance();}}

        public Frm_Customer()
        {
            InitializeComponent();
        }

        protected override void InitFields()
        {
            spe_CFlightIntergralRate.Properties.DisplayFormat.FormatString = "#,0.####";
            spe_CFlightIntergralRate.Properties.EditFormat.FormatString = "#,0.####";
            spe_HotelIntergralRate.Properties.DisplayFormat.FormatString = "#,0.####";
            spe_HotelIntergralRate.Properties.EditFormat.FormatString = "#,0.####";
        }

        protected override void Events()
        {
            this.gv_Customers.FocusedRowChanged += (s, e) =>
                {
                    Contract.RCustomer customer = gv_Customers.GetRow(e.FocusedRowHandle) as Contract.RCustomer;

                    if (customer == null)
                        SelCust = null;
                    else
                        SelCust = _masterProxy.GetCustomerByID(customer.ID).ReturnValue;
                };
            this.gv_Passengers.FocusedRowChanged += (s, e) =>
                {
                    SelPass = gv_Passengers.GetRow(e.FocusedRowHandle) as Passenger;
                };
            this.gv_Passengers.CustomDrawCell += (s, e) =>
                {
                    this.gv_Passengers.CustomDrawCell<Passenger>(e,
                        (pass) =>
                        {
                            return !pass.Effective;
                        },
                        () =>
                        {
                            e.Appearance.ForeColor = Color.Red;
                        });

                    if (e.Column == colTel)
                    {
                        Passenger passenger = this.gv_Passengers.GetRow(e.RowHandle) as Passenger;

                        if (passenger == null)
                            return;

                        if (!string.IsNullOrEmpty(passenger.Tel1) && !string.IsNullOrEmpty(passenger.Tel2))
                            e.DisplayText = string.Format("{0} ; {1}", passenger.Tel1, passenger.Tel2);
                        else
                            e.DisplayText = string.IsNullOrEmpty(passenger.Tel1) ? passenger.Tel2 : passenger.Tel1;
                    }
                };
        }

        protected override void LoadDatas()
        {
            this.bds_Customer.BindingData(_masterProxy.GetAllCustomer().ReturnValue);
            


            foreach (Contract.RoleAuthorizedItem roleAuthorizedItem in AppParameters.LoginUser.Role.Authorities)
            {
                switch (roleAuthorizedItem.AuthorizedItem.Name)
                {
                    case "新增客户":
                        bbt_Add.Enabled = roleAuthorizedItem.Allow;
                        break;
                    case "修改客户":
                        bbt_Edit.Enabled = roleAuthorizedItem.Allow;
                        bbt_Delete.Enabled = roleAuthorizedItem.Allow;
                        break;
                    default:
                        break;
                }
            }
        }

        private void bar_Main_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            switch (e.Item.Name)
            {
                case "bbt_Add":
                    this.add();                    
                    break;
                case "bbt_Edit":
                    this.edit();                    
                    break;
                case "bbt_Close":
                    this.Close();
                    break;
                case "bbt_Delete":
                    this.delete();
                    break;
                case "bbt_积分兑换历史":
                    积分兑换历史();
                    break;
                default:
                    break;
            }

        }

        private void add()
        {
            Frm_CustomerEdit frm = new Frm_CustomerEdit();
            frm.Text = "增加客户";
            frm.Load += (s, e) =>
                {
                    frm.CurCust = new Contract.Customer();
                };
            FormOpener.ShowDialog(frm);
        }

        private void edit()
        {
            Frm_CustomerEdit frm = new Frm_CustomerEdit();
            frm.Text = "修改客户资料";
            frm.Load += (s, e) =>
                {
                    frm.CurCust = SelCust;
                };
            FormOpener.ShowDialog(frm);
            onSelCustChanged();
        }

        private void 积分兑换历史()
        {
            if (SelCust != null)
            {
                Frm_积分兑换历史 frm = new Frm_积分兑换历史();
                frm.Customer = SelCust;
                FormOpener.ShowDialog(frm);
            }
        }

        private void delete()
        {
            if (this.SelCust == null)
                return;

            MsgBox.Question(string.Format( "确定删除编号 {0} 的客户资料吗?",SelCust.Nr),()=>
            {
                var result = _masterProxy.DeleteCustomer(this.SelCust);
                if (result.IsSuccess) 
                {
                    MsgBox.Info("删除成功.");
                    this.bds_Customer.RemoveCurrent();
                }
            },null);
        }

        //selected customer
        private Contract.Customer _selCust;
        public Contract.Customer SelCust
        {
            get { return _selCust; }
            set 
            {
                _selCust = value;
                onSelCustChanged();
            }
        }

        void onSelCustChanged()
        {
            if (SelCust == null)
            {
                // clear customer datas
                ControlExtensions.ClearValue(
                        this.txt_CAddress,
                        this.txt_CContactPerson1,
                        this.txt_CContactPerson2,
                        this.txt_CContactPersonPhone1,
                        this.txt_CContactPersonPhone11,
                        this.txt_CContactPersonPhone2,
                        this.txt_CContactPersonPhone22,
                        this.txt_CContactPersonTel1,
                        this.txt_CContactPersonTel2,
                        this.txt_CFax,
                        this.txt_CFinancialAffairs,
                        this.txt_CFinancialAffairsPhone1,
                        this.txt_CFinancialAffairsTel,
                        this.txt_CIntegral,
                        this.txt_CName,
                        this.txt_CNr,
                        this.txt_CRemark,
                        this.txt_Salesman,
                        this.txtAmount,
                        this.txt_WebUser
                    );

                this.bds_Passenger.DataSource = null;
            }
            else
            {
                this.gv_Passengers.FocusedRowHandle = -1;

                // set customer datas
                this.txtAmount.SetText(SelCust.Amount);
                this.txt_CAddress.SetText(SelCust.Address);
                this.txt_CContactPerson1.SetText(SelCust.ContactPerson1);
                this.txt_CContactPerson2.SetText(SelCust.ContactPerson2);
                this.txt_CContactPersonPhone1.SetText(SelCust.ContactPerson1Phone1);
                this.txt_CContactPersonPhone11.SetText(SelCust.ContactPerson1Phone2);
                this.txt_CContactPersonPhone2.SetText(SelCust.ContactPerson2Phone1);
                this.txt_CContactPersonPhone22.SetText(SelCust.ContactPerson2Phone2);
                this.txt_CContactPersonTel1.SetText(SelCust.ContactPerson1Tel);
                this.txt_CContactPersonTel2.SetText(SelCust.ContactPerson2Tel);
                this.txt_CFax.SetText(SelCust.Fax);
                this.txt_CFinancialAffairs.SetText(SelCust.FinancialAffairs);
                this.txt_CFinancialAffairsPhone1.SetText(SelCust.FinancialAffairsPhone1);
                this.txt_CFinancialAffairsPhone2.SetText(SelCust.FinancialAffairsPhone2);
                this.txt_CFinancialAffairsTel.SetText(SelCust.FinancialAffairsTel);
                this.txt_CIntegral.SetText(Convert.ToString(SelCust.Intergral));
                this.txt_CName.SetText(SelCust.Name);
                this.txt_CNr.SetText(SelCust.Nr);
                this.txt_CRemark.SetText(SelCust.Remark);
                if(SelCust.WebUser != null)
                    this.txt_WebUser.SetText(SelCust.WebUser.Name);
                else
                    this.txt_WebUser.SetText(string.Empty);

                if(SelCust.Salesman != null)
                    this.txt_Salesman.SetText(SelCust.Salesman.Staff);

                this.spe_CFlightIntergralRate.SetValue(SelCust.FlightIntergralRate);

                if (SelCust.HotelIntergralList.Count == 0 && SelCust.HotelIntergralRate.HasValue)
                {
                    rad_Price.Checked = true;
                }
                else if (SelCust.HotelIntergralList.Count > 0)
                {
                    rad_HotelLevel.Checked = true;
                }


                this.bds_customerHotelIntergralRate.DataSource = SelCust.HotelIntergralList;
                if(SelCust.HotelIntergralRate != null)
                spe_HotelIntergralRate.SetValue(SelCust.HotelIntergralRate.Value);
                else
                    spe_HotelIntergralRate.SetValue(0);

                this.bds_Passenger.DataSource = SelCust.PassengerList;
                this.gv_Passengers.FocusedRowHandle = 0;
                
            }
        }

        #region Passenger

        // selected passenger
        private Passenger _selPass;
        public Passenger SelPass
        {
            get { return _selPass; }
            set
            {
                _selPass = value;
                onSelPassChanged();
            }
        }

        private void onSelPassChanged()
        {
            if (SelPass == null)
            {
                // clear passenger datas
                ControlExtensions.ClearValue(
                        this.txt_PCardNr1,
                        this.txt_PCardNr2,
                        this.txt_PCardType1,
                        this.txt_PCardType2,
                        this.txt_PCName,
                        this.txt_PEName,
                        this.txt_PFightCard1,
                        this.txt_PFightCard2,
                        this.txt_PFightCard3,
                        this.txt_PNation,
                        this.txt_PSex,
                        this.txt_PTel1,
                        this.txt_PTel2,
                        this.dte_PBirthday,
                        this.txt_CardEffective1,
                        this.txt_CardEffective2
                    );
            }
            else
            {
                // set passenger datas
                this.txt_PCardNr1.SetText(SelPass.IDCardNr1);
                this.txt_PCardNr2.SetText(SelPass.IDCardNr2);
                this.txt_PCardType1.SetText(SelPass.IDCardType1);
                this.txt_PCardType2.SetText(SelPass.IDCardType2);
                this.txt_PCName.SetText(SelPass.CName);
                this.txt_PEName.SetText(SelPass.EName);
                this.txt_PFightCard1.SetText(SelPass.FlightCard1);
                this.txt_PFightCard2.SetText(SelPass.FlightCard2);
                this.txt_PFightCard3.SetText(SelPass.FlightCard3);
                this.txt_PNation.SetText(SelPass.Nation);
                this.txt_PSex.SetText(SelPass.Sex);
                this.txt_PTel1.SetText(SelPass.Tel1);
                this.txt_PTel2.SetText(SelPass.Tel2);
                this.dte_PBirthday.SetValue(SelPass.BirthDay);
                this.txt_CardEffective1.SetText(SelPass.IDCardEffective1);
                this.txt_CardEffective2.SetText(SelPass.IDCardEffective2);
            }
        }

        #endregion


    }
}
